package com.itcast.rpc.core.client.response;

import com.itcast.rpc.core.client.NettyRpcClient;

import java.lang.reflect.Proxy;

/**
 * 通过动态代理生成代理对象
 */
public class BeanFactory {
    
    private NettyRpcClient nettyClient;
    
    public BeanFactory(NettyRpcClient nettyClient) {
        this.nettyClient = nettyClient;
    }
    
    public <T> T getBean(Class<T> clazz) {
        return (T) Proxy.newProxyInstance(
                Thread.currentThread().getContextClassLoader(),
                new Class<?>[]{clazz},
                new ClientInvocationHandler(nettyClient)
        );
    }
    
}